home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amoszine 11
/
Amoszine 11 (Disk 2 of 2).adf
/
Ben_Wyatt_Source.lha
/
Fast_Sort.AMOS
/
Fast_Sort.amosSourceCode
Wrap
AMOS Source Code
|
2004-04-12
|
1KB
|
50 lines
' Fast sort procedure
' ~~~~~~~~~~~~~~~~~~~
' by Ben Wyatt, bwyatt@paston.co.uk
'
' Sorts a list of numbers that have a known maximum value
' As it's name suggests, it's very fast - faster even than the Sort command!
' It was also invented by me 8-)
' Reserve room for the massive list of numbers to sort
Set Buffer 200
NUMBERS=50000 : MXVALUE=200
Dim LIST(NUMBERS)
Global LIST()
Print "Making up some random numbers"
For N=0 To NUMBERS
LIST(N)=Rnd(MXVALUE)
Next N
Print "Now sorting"+Str$(NUMBERS)+" numbers"
Timer=0
_FSORT[MXVALUE,0,NUMBERS]
Print "Sorted in"+Str$(Timer/50.0)+" seconds"
Procedure _FSORT[MXVALUE,STNUM,ENNUM]
' Sorts array LIST(), that contains the maximum value of MXVALUE
' between element STNUM and ENNUM
' Reserve some memory for the number of each number :-?
Dim CNT(MXVALUE)
' Count the occurances of each number
For N=STNUM To ENNUM
Inc CNT(LIST(N))
Next N
' Put the numbers back into the array, but sorted
ELEMENT=STNUM
For N=0 To MXVALUE
If CNT(N)>0
For M=1 To CNT(N)
LIST(ELEMENT)=N : Inc ELEMENT
Next M
End If
Next N
End Proc